<?php

/**
 * 自动生成 WxEventCupidAwardService
 * 
 * Apps/Common/Service/WxEventCupidAwardService.class.php
 * Zix @ 2015-08-12
 */

namespace Common\Service;

class WxEventCupidAwardService {

  private static $instance;
  var $rank = array(
    1 => '一等奖',
    2 => '二等奖',
    3 => '三等奖',
    4 => '四等奖',
    5 => '五等奖',
    6 => '六等奖',
    7 => '七等奖',
    8 => '八等奖',
    9 => '九等奖',
  );

  public static function instance() {
    if (self::$instance == null) {
      $instance = new WxEventCupidAwardService();
    }

    return $instance;
  }

  function get_default_row() {
    return array(
      'name' => '',
      'rank' => 1,
      'min_point' => 0,
      'max_point' => 1,
      'total_qty' => 1,
      'taken_qty' => 0,
      'remain_qty' => 0 ,
      'take_rate' => 0,
    );
  }

  function get_by_id( $id ) {
    $WxEventCupidAward = M('WxEventCupidAward');
    $data = $WxEventCupidAward->find($id);
    return $data ? $data : array();
  }

  function get_by_cond($config) {
    $default = array(
      'event_type' => '',
      'event_id' => '',
      'have_remain' => 0 ,
    );

    $config = extend($config, $default);

    $WxEventCupidAward = M('WxEventCupidAward');

    $where = array();

    if (!empty($config['event_type'])) {
      $where['event_type'] = $config['event_type'];
    }

    if (!empty($config['event_id'])) {
      $where['event_id'] = $config['event_id'];
    }
    
    if (!empty($config['have_remain'])) {
      $where['remain_qty'] = array('gt' , 0 );
    }
    
    if ($config['count']) {
      return $WxEventCupidAward->where($where)->count();
    } else {
      $data = $WxEventCupidAward
          ->where($where)
          ->order('rank asc')->select();
      //echo $WxEventAward->_sql();
      return $data ? $data : array();
    }
  }
  
  function get_by_rank( $event_type , $event_id , $rank ) {
    $WxEventAward = M('WxEventAward');
    $where = array(
      'event_type' => $event_type ,
      'event_id' => $event_id ,
      'rank' => $rank ,
    );
    
    $data = $WxEventAward->where( $where )->find();
    return $data ? $data : array();
  }
  
  function create($data, $is_ajax = true) {
    $old_data = $this->get_by_rank($data['event_type'], $data['event_id'], $data['rank']);
    if ( !empty( $old_data ) ) {
      return ajax_arr( $this->rank[$data['rank']] . '已经有了') ;
    }
    
    $WxEventCupidAward = M('WxEventCupidAward');
    $ret = $WxEventCupidAward->add($data);
    if ($is_ajax) {
      if ($ret) {
        return ajax_arr('添加成功', TRUE, array(
          'id' => $ret
        ));
      } else {
        return ajax_arr('添加失败', FALSE);
      }
    } else {
      if ($ret) {
        return $ret;
      } else {
        return 0;
      }
    }
  }

  function update($id, $data) {
    $old_data = $this->get_by_rank($data['event_type'], $data['event_id'], $data['rank']);
    if ( !empty( $old_data ) && $old_data['id'] != $id ) {
      return ajax_arr( $this->rank[$data['rank']] . '已经有了' ) ;
    }
    
    $WxEventCupidAward = M('WxEventCupidAward');

    $ret = $WxEventCupidAward->where("id = %d", $id)->save($data);
    if ($ret) {
      return ajax_arr('编辑成功', TRUE);
    } else {
      return ajax_arr('编辑失败', FALSE);
    }
  }

  function delete($ids) {
    $WxEventCupidAward = M('WxEventCupidAward');
    $ret = $WxEventCupidAward->delete($ids);
    if ($ret == 0) {
      return ajax_arr('未删除任何数据', FALSE);
    } else if (!$ret) {
      return ajax_arr('删除失败', FALSE);
    } else {
      return ajax_arr('删除' . $ret . '行数据', TRUE);
    }
  }

}
